package controllers;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpRequest;

import play.mvc.Controller;
import play.mvc.Http.Request;
import services.SystemParamService;
import services.WeChatService;
import utils.Base64Util;
import utils.BizConstants;
import utils.wechat.MenuUtil;
import utils.wechat.MessageUtil;
import utils.wechat.SHA1;
import vos.CountMonthVo;
import vos.weChat.TextMessage;

public class WeChatController extends Controller {
	
	/**
	 * 测试来源是否为微信
	 *
	 * @Title: TestLink 
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @param echostr
	 * @return: void
	 */
	public static void TestLink(String signature,String timestamp,String nonce,String echostr){
		if(StringUtils.isEmpty(signature)){
			renderJSON(false);
		}
		String token="123456";
		String[] str={token,timestamp,nonce};
		Arrays.sort(str);
	    String bigStr=str[0]+str[1]+str[2];
        String digest = new SHA1().getDigestOfString(bigStr.getBytes()).toLowerCase(); 
		if(signature.equals(digest)){
			renderJSON(echostr);
		}
		else{
			renderJSON(false);
		}
	}
	/**
	 * 获取返回的信息
	 *
	 * @Title: getMessage 
	 * @return: void
	 */
	public static void getMessage(){
		 //String respMessage=""; 
		 String beginwork=SystemParamService.getByCacheId(BizConstants.BEGIN_WORK_TIME);
		 String endwork=SystemParamService.getByCacheId(BizConstants.END_WORK_TIME);
		 String message=WeChatService.processRequest(beginwork,endwork,request);
		 renderXml(message);
	}
}
